package com.example.twoeightgang;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BaoCunDialog extends AlertDialog implements View.OnClickListener {
    private Context mContext;
    private Button btOK;
    private Button btCancel;
    private TextView textView;
    private EditText editText;
    private String name;
    private Editable number1;

    protected BaoCunDialog(Context context) {
        super(context);
    }

    protected BaoCunDialog(Context context, int number) {
        super(context, number);
    }

    protected BaoCunDialog(Context context, String name) {
        super(context);
        this.mContext=context;
        this.name=name;
    }

    protected BaoCunDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baocun_layout);
        initDate();
    }

    protected void initDate() {
        btOK=(Button)findViewById(R.id.queren);
        btCancel=(Button)findViewById(R.id.quxiao);
        textView=(TextView)findViewById(R.id.chongming);
        editText=(EditText)findViewById(R.id.chongmingming);
        //editText.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
        editText.setText(name + "");
        number1=editText.getText();
        Selection.setSelection(number1, number1.length());
        btOK.setOnClickListener(this);
        btCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.queren:
//                number=Integer.parseInt(number1.toString());
//                Intent mIntent = new Intent("com.ykk.change");
//                mIntent.putExtra("yaner", number);
//                if(number>10000){
//                    Toast.makeText(mContext, mContext.getResources().getString(R.string.tishi) + number1.toString() ,Toast.LENGTH_SHORT).show();
//                }else {
//                    MainActivity.yazhu=number;
//                    Log.d("ykk", "queren == " + number);
//                    mContext.sendBroadcast(mIntent);
//                    dismiss();
//                }
                name=number1.toString();
                if(name!=null && !name.trim().equals("")){
                    Intent mIntent = new Intent("com.ykk.baocunnoexit");
                    mIntent.putExtra("yaner", name);
                    mContext.sendBroadcast(mIntent);
                    Toast.makeText(mContext, mContext.getResources().getString(R.string.savename) +  name, Toast.LENGTH_SHORT).show();
                    dismiss();
                }else{
                    Toast.makeText(mContext, mContext.getResources().getString(R.string.tishi1),Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.quxiao:
//                Log.d("ykk","quxiao == " +  number);
                dismiss();
                break;
            default:
                break;
        }
    }
}
